<template>
  <!-- 用户页顶部条 -->
  <div style="height: 6vh;background-color:#fff;padding:10px 20px;">
    <span style="line-height:60px;font-size: 20px;">住户管理</span>
    <el-button @click="dialogVisible=true" type="primary"
               style="float:right;margin-top:13px;">新建住户</el-button>
  </div>
  <!-- 新增用户弹窗 -->
  <el-dialog :title="dialogTitle" v-model="dialogVisible" :before-close="handleClose"
             style="width:1000px;padding:40px;">
    <el-form label-width="80px" label-position="top">
      <el-row :gutter="10">
        <el-col :span="24">
          <el-form-item label="住户名">
            <el-input placeholder="请输入住户名" v-model="saveUserForm.lname"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
        <el-row :gutter="10">
        <el-col :span="24">
          <el-form-item label="身份证">
            <el-input placeholder="请输入身份证" v-model="saveUserForm.lcard"></el-input>
          </el-form-item>
        </el-col>
        </el-row>
        <el-row :gutter="10">
        <el-col :span="24">
          <el-form-item label="手机号">
            <el-input placeholder="请输入手机号" v-model="saveUserForm.lphone"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="10">
        <el-col :span="24">
          <el-form-item label="房间">
            <el-input placeholder="请输入房间" v-model="saveUserForm.lhome"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="10">
        <el-col :span="24">
          <el-form-item label="业主入住时间">
            <el-date-picker type="date" format="YYYY-MM-DD" value-format="YYYY-MM-DD"
                            v-model="saveUserForm.ltime"/>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <el-button @click="handleClose">取消</el-button>
      <el-button type="primary" @click="saveUser">保存</el-button>
    </template>
  </el-dialog>

  <!-- 用户查询卡片 -->
  <el-card style="margin: 20px;height: 70px;">
    <el-form :inline="true">
      <el-form-item label="住户搜索">
        <el-input placeholder="请输入用户名"
                  style="width: 220px;" v-model="searchUserForm.lname"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="resetSearch">重置</el-button>
        <el-button type="primary" @click="loadUser">查询</el-button>
      </el-form-item>
    </el-form>
  </el-card>
  <!-- 显示用户信息表格 -->
  <el-card style="margin:20px;">
    <el-table :data="userArr">
      <el-table-column type="index" label="序号" width="80" align="center"></el-table-column>
      <el-table-column prop="lname" label="住户名" align="center"></el-table-column>
      <el-table-column prop="lcard" label="证件号码" align="center"></el-table-column>
      <el-table-column prop="lphone" label="手机号" align="center"></el-table-column>
      <el-table-column prop="lhome" label="房间" align="center"></el-table-column>
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button link type="primary" size="small"
                     :disabled="scope.row.level==40" @click="editUser(scope.row.lid)">编辑</el-button>
          <el-button link type="primary" size="small"
                     :disabled="scope.row.level==40" @click="deleteUser(scope.row.lid)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-card>

</template>



<script setup>
import {onMounted, ref} from "vue";
import qs from "qs";
import axios from "axios";
import {ElMessage} from "element-plus";

//定义变量控制弹窗标题
const dialogTitle = ref('新建用户');
//定义变量控制弹窗是否显示
const dialogVisible = ref(false);
//定义数组保存直属领导数据
const leaderOption = ref([
  {username:'shaoyun',id:1},
  {username:'mike',id:2}
]);

//准备对象用来收集新增/修改员工的表单信息
const saveUserForm = ref({
  lname:'',
  lid:'',
  lcard:'',
  lphone:'',
  lhome:'',
  ltime:''
});


//新增+更新员工
const saveUser = ()=>{
  let data = qs.stringify(saveUserForm.value);
  console.log(data);
  axios.post(BASE_URL+'/v1/workspace/save',data).then((response)=>{
    if(response.data.code==2000){
      ElMessage.success('保存成功!');
      dialogVisible.value = false;//关闭弹窗
      saveUserForm.value = {};//清空表单输入数据,防止新增成功后还是上一个用户的数据
      loadUser();
    }else{
      ElMessage.error(response.data.msg);
    }
  })
}

//实现关闭功能
const handleClose = ()=>{
  if (confirm('是否关闭本窗口?')){
    //用户确认关闭后,将弹窗关闭且将表单数据清空
    dialogVisible.value=false;
    saveUserForm.value = {};
  }
}

//定义数组用来装用户列表数据
const userArr = ref([]);
//定义对象保存查询条件
const searchUserForm = ref({
  lname:'',
  lcard:'',
  lphone:'',
  lhome:'',
  ltime:''
  })
/*const search = ref({lname:''});*/
//定义加载用户的方法
const loadUser = ()=>{
  //有条件则根据条件查员工数据,没有条件默认查所有员工数据
  let data = qs.stringify(searchUserForm.value);
  axios.get(BASE_URL+'/v1/workspace/select?'+data).then((response)=>{
    if(response.data.code==2000){
      userArr.value = response.data.data;
    }else{
      ElMessage.error(response.data.msg);
    }
  })
  console.log(data);
}
//页面一加载立即请求加载用户数据
onMounted(()=>{
  loadUser();
})
//重置用户搜索信息
const resetSearch = ()=>{
  searchUserForm.value = {};//清空用户搜索表单数据
  loadUser();//重新加载用户数据
}

//编辑住户
const editUser = (lid)=>{
  //设置弹窗出现且修改弹窗标题
  dialogVisible.value = true;
  dialogTitle.value = '编辑住户';
  //根据id获取当前要修改的用户信息
  axios.get(BASE_URL+'/v1/workspace/select?lid='+lid).then((response)=>{
    if (response.data.code==2000){
      //如果请求成功,后端返回的是list,我们需要唯一的那条数据,所以需要data[0]
      saveUserForm.value = response.data.data[0];
    }else{
      ElMessage.error(response.data.msg);
    }
  })
}
//删除用户方法
const deleteUser = (lId)=>{
  console.log(lId);
  if (confirm('您确认要删除该用户吗?')){
    axios.post(BASE_URL+'/v1/workspace/delete/'+lId).then((response)=>{
      if (response.data.code==2000){
        ElMessage.success('删除成功!');
        loadUser();
      }else{
        ElMessage.error(response.data.msg);
      }
    })
  }
}

</script>

<style scoped>

</style>